#!/usr/bin/python
#
# hephaestus.py
# Copyright (C) Steven Crawford 2008 <steve.g.crawford@gmail.com>
# 
# hephaestus.py is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# hephaestus.py is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import irclib
import gobject
import pygtk
import gtk
import gtk.glade
import threading
from string import atoi

# Glade File
gladefile="hephaestus.glade"

# Some line of code...
irclib.DEBUG = True
gobject.threads_init()

# Main Window Class
class gui:
  def __init__(self):
    windowname="mainwindow"
    self.wTree=gtk.glade.XML(gladefile,windowname)
    
    dic = { "on_sendButton_clicked" : \
            (self.sendButton_activate),
            "on_sendButton_activate" : \
            (self.sendButton_activate),
            "on_textEntry_activate" : \
            (self.sendButton_activate),
            "on_imagemenuitem10_activate" : \
            (self.aboutmenu_activate),
            "on_imagemenuitem5_activate" : \
            (gtk.main_quit),
            "on_mainwindow_destroy" : \
            (gtk.main_quit) }
    self.wTree.signal_autoconnect (dic)
    return

# Callbacks
# Send Button
  def sendButton_activate(self,widget):
    entrytext=self.wTree.get_widget("textEntry").get_text()
    textview=self.wTree.get_widget("textView")
    viewbuffer=textview.get_buffer()
    textview.set_buffer(viewbuffer)
    # Handle Commands
    if entrytext.startswith('/'):
      splitmessage=entrytext.split(' ')
      # /connect
      if splitmessage[0] == "/connect":
        SERVER = splitmessage[1]
        PORT = splitmessage[2]
        NICK = splitmessage[3]
        PORT = atoi(PORT)
        server.connect(SERVER, PORT, NICK)
        print "CONNECTED"
      # /join
      elif splitmessage[0] == "/join":
        CHANNEL = splitmessage[1]
        server.join(CHANNEL)
        return CHANNEL
      # /disconnect
      elif splitmessage[0] == "/disconnect":
        server.disconnect()
        print "DISCONNECTED"
      # /clear
      elif splitmessage[0] == "/clear":
        viewbuffer.set_text("")
        textview.set_buffer(viewbuffer)
      # /testconn
      elif splitmessage[0] == "/testconn":
        if irc.is_connected() == 1:
          print "CONNECTED"
        else:
          print "NOT CONNECTED"
    else:
      server.privmsg(CHANNEL, entrytext)

# About Dialog Menu Item
  def aboutmenu_activate(self,widget):
    about=aboutdlg()
    about.run()

# GTK Main Loop
  def main(self):
    gtk.main()
    return 0
    
# Channel Error Class
#class errchan:
#  def __init__(self):
#    windowname="errChannel"
#    self.wTree=gtk.glade.XML(gladefile,windowname)
#    return
#    
#  def run(self):
#    self.dlg = self.wTree.get_widget("errChannel")
#    self.vbox = self.wTree.get_widget("vbox2")
#    self.button = self.wTree.get_widget("errChannelButton")
#    self.label = self.wTree.get_widget("errChannel")

# About Dialog Class
class aboutdlg:
  def __init__(self):
    windowname="about"
    self.wTree=gtk.glade.XML(gladefile,windowname)
    return

  def run(self):
    self.dlg = self.wTree.get_widget("about")
    self.label1 = self.wTree.get_widget("abouttxt")

# Threads
class GUIThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
  
  def run(self):
    gui.main()
    
class IRCThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)

  def run(self):
    irc.process_forever()

# App Start
if __name__ == "__main__":
  gui=gui()
  irc=irclib.IRC()
  server=irc.server()
  GUIThread().start()
  IRCThread().start()
